package cheme.ProcessEditor.Parts.Equations;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

import cheme.ProcessEditor.Policies.ToolbarLayoutEditPolicy;
import cheme.ProcessModel.IChild;
import cheme.ProcessModel.IParent;
import cheme.ProcessModel.Cmds.AddCmd;
import cheme.ProcessModel.Cmds.MoveCmd;
import cheme.ProcessModel.Equations.Equation;
import cheme.ProcessModel.Equations.Equations;

public class PolicyEquationListLayout extends ToolbarLayoutEditPolicy
{
	/*
	 * Commands that may reasonably yields target feedback:
	 * REQ_ADD
	 * REQ_CLONE
	 * REQ_MOVE
	 * REQ_RESIZE_CHILDREN
	 * REQ_CREATE
	 */
	@Override
	protected void showLayoutTargetFeedback(Request request)
	{
		boolean allowShow = false;
		
		if(REQ_CREATE.equals(request.getType()))
		{
			if((((CreateRequest)request).getNewObject() instanceof Equation))
			{
				allowShow = true;
			}
		}
		else if(REQ_ADD.equals(request.getType()) || REQ_MOVE.equals(request.getType()))
		{
			List<GraphicalEditPart> parts = ((ChangeBoundsRequest)request).getEditParts();
			
			if(parts.size() > 0)
			{
				/*
				 * All model elements have to be of TagComponent type
				 */
				
				allowShow = true;
				
				for(GraphicalEditPart part : parts)
				{
					allowShow &= (part.getModel() instanceof Equation);
				}			
			}
		}
		
		if(allowShow)
		{
			super.showLayoutTargetFeedback(request);
		}
	}
	
	@Override
	protected Command createAddCommand(EditPart child, EditPart after)
	{
		if (!(child instanceof PartEquation))
		{
			return null;
		}
		
		if (!(after instanceof PartEquation))
		{
			return null;
		}

		Equation moveChild = (Equation) child.getModel();
		Equation afterChild = (Equation) after.getModel();

		PartEquations originalPart = (PartEquations) child.getParent();
		Equations originalParent = originalPart.getModel();
		PartEquations newPart = (PartEquations) after.getParent();
		Equations newParent = newPart.getModel();

		MoveCmd command = new MoveCmd(moveChild, afterChild, originalParent, newParent);
		
		return command;
	}

	@Override
	protected Command createMoveChildCommand(EditPart child, EditPart after)
	{		
		int newIndex = -1;
		
		if (after != null)
		{
//			IStreamComponentDesc afterModel = (IStreamComponentDesc) after.getModel();
			newIndex = getHost().getChildren().indexOf(after);
		}
		
		IChild childModel = (IChild) child.getModel();
		IParent parent = (IParent) getHost().getModel();
		
//		int oldIndex = getHost().getChildren().indexOf(child);

		MoveCmd command = new MoveCmd(childModel, parent, newIndex);
		
		return command;
	}

	/**
	 * @param request
	 * @return
	 */
	@Override
	protected Command getCreateCommand(CreateRequest request)
	{
		Command cmd = null;
		
		if(request.getNewObject() instanceof IChild)
		{
			IChild newChild = (IChild)request.getNewObject();
			
			EditPart after = getInsertionReference(request);
			
			int insertIndex = getHost().getChildren().indexOf(after);
			
			cmd = 
				new AddCmd(newChild, (IParent)getHost().getModel(), insertIndex);
		}
		
		return cmd;
	}

	/**
	 * @param request
	 * @return
	 */
	protected Command getDeleteDependantCommand(Request request)
	{
		return null;
	}

}